package com.ruoyi.carUser.service.imp;

import com.ruoyi.carUser.domain.UserCoupon;
import com.ruoyi.carUser.mapper.CouponUserMapper;
import com.ruoyi.carUser.mapper.OrderMapper;
import com.ruoyi.carUser.service.OrderService;
import com.ruoyi.carUser.util.FileUploadController;
import com.ruoyi.system.api.domain.OrderDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public OrderDetails selectOrderDetails(OrderDetails orderDetails) {
        return orderMapper.selectOrderDetails(orderDetails);
    }
    @Autowired
    private CouponUserMapper couponUserMapper;
    @Override
    public int insertOrder(OrderDetails orderDetails) {
        if (orderDetails.getCouponId()!=null){
            System.out.println("将对应的优惠券的状态值改为已核销");
            //先查询符合条件的优惠券再讲查出的第一个优惠券变为已核销状态
            List<UserCoupon> list = couponUserMapper.list(orderDetails.getCouponId());
            if (list.size()>0){
                UserCoupon userCoupon = list.get(0);
                userCoupon.setNum( userCoupon.getNum()-1);
                couponUserMapper.updateUserCoupon(userCoupon);
            }
        }
        return orderMapper.addOrder(orderDetails);
    }

    @Override
    public int updateCodeAndImg(OrderDetails orderDetails) {
        orderDetails.setPaytus("已支付");
        return orderMapper.updateCodeAndImg(orderDetails);
    }
}
